<template>
  <el-dialog title="审核促销活动" v-el-drag-dialog :visible.sync="editVisible">
    <el-container>
      <el-main class="popup_content">
        <el-form ref="editPromotion" :model="editPromotion"  label-width="auto" size="mini">
          <el-form-item label="类型：" prop="type">
            <span>{{editPromotion.type|watchType}}</span>
          </el-form-item>
          <el-form-item label="促销商品：" prop="title"   v-if="editPromotion.type==7||editPromotion.type==8||editPromotion.type==9||editPromotion.type==10">
            <span>{{editPromotion.product.name}}</span>
          </el-form-item>
          <el-form-item label="促销标题：" prop="title" >
            <span>{{editPromotion.title}}</span>
          </el-form-item>
          <el-form-item label="购买数量：" prop="minimumPrice"
                        v-if="editPromotion.type!=2||editPromotion.type!=8||editPromotion.type!=9||editPromotion.type!=10">
          <span>{{editPromotion.minimumPrice}}</span>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="满折比例：" prop="agioRate" v-if="editPromotion.type===4">
                <span>{{editPromotion.agioRate}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="editPromotion.type  |watchAmountType" prop="amount" v-if="editPromotion.type===3||editPromotion.type===7">
                <span>{{editPromotion.amount}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="促销主图：" prop="thumbnail" v-if="editPromotion.type===0||editPromotion.type===2">
          <img width="30%" height="30%" :src="editPromotion.thumbnail">
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="只适用于首单生效：" prop="firsted"
                            v-if="editPromotion.type===3||editPromotion.type===5||editPromotion.type===4">
                <span>{{editPromotion.firsted|watchTrue}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="活动是否跟单品活动叠加：" prop="overlying"
                            v-if="editPromotion.type===3||editPromotion.type===5||editPromotion.type===4">
                <span>{{editPromotion.overlying|watchTrue}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="editPromotion.type===0||editPromotion.type===1||editPromotion.type===2||editPromotion.type===5 && editPromotion.gifts.length>0">
            <el-col :span="12">
              <el-form-item label="赠品类型：" prop="giftType" >
                <span>{{editPromotion.giftType|watchGiftType}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <editGift :editGiftType="editPromotion.giftType" :editGift="editPromotion.gifts"  v-if="editPromotion.giftType===0" :isStatus="isStatus" ref="editChild"></editGift>
          <editCoupon v-if="editPromotion.giftType===1" :editCoupon="editPromotion.gifts" :isStatus="isStatus"  ref="editCouponAdd"></editCoupon>
          <el-form-item label="优先级：" prop="orders">
            <span>{{editPromotion.orders}}</span>
          </el-form-item>
          <el-form-item label="开始时间：" prop="beginDate" v-if="editPromotion.beginDate">
            <span>{{editPromotion.beginDate|datetimeFormatter}}</span>
          </el-form-item>
          <el-form-item label="结束时间：" prop="beginDate" v-if="editPromotion.endDate">
            <span>{{editPromotion.endDate|datetimeFormatter}}</span>
          </el-form-item>
          <el-form-item label="促销说明：" prop="introduction" v-if="editPromotion.type!=7&&editPromotion.type!=8&&editPromotion.type!=9&&editPromotion.type!=10">
            <span>{{editPromotion.introduction}}</span>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <div slot="footer" class="dialog-footer">
      <el-button @click="editVisible = false">取 消</el-button>
      <el-button type="primary" @click="updatePromotion('editPromotion')">审 核</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {EventBus} from '@/api/eventBus'
  import { find, updatePromotion } from '../../../api/promotion'
  import giftList from './gift'
  import coupon from './coupon'
  import SingleUpload from '@/components/Upload/singleUpload'
  import editGift from './editGift'
  import editCoupon from './editCoupon'
  import elDragDialog from '@/directive/el-drag-dialog'
  export default {
    name: 'promotion-examine',
    directives: {elDragDialog},
    data(){
      return{


        editVisible:false,
        dateTime:[],
        isStatus:false,
        giftTypeList:[],
        editPromotion:{
          id:'',
          givenAmount:0,
          agioRate:0,
          amount:0,
          firsted:false,
          giftType:'',
          gifts:[
            {
              promotionId:'',
              giftId:'',
              availableQuantity:0,
              giftName:'',
              quantity:0}
          ],
          introduction:'',
          minimumPrice:0,
          modifyDate:'',
          orders:0,
          overlying:false,
          thumbnail:'',
          title:'',
          type:0
        },
      }
    },
    methods:{
      updatePromotion(editPromotion){
        this.$refs[editPromotion].validate((valid) =>{
          if (valid){
            this.$confirm('确认审核?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              var editPromotion={
                id:this.editPromotion.id,
                agioRate:this.editPromotion.agioRate,
                amount:this.editPromotion.amount,
                firsted:this.editPromotion.firsted,
                giftType:this.editPromotion.giftType,
                gifts:[],
                beginDate:'',
                endDate:'',
                status:1,
                goodsId: this.editPromotion.goodsId,
                introduction:this.editPromotion.introduction,
                minimumPrice:this.editPromotion.minimumPrice,
                modifyDate:new Date(),
                orders:this.editPromotion.orders,
                overlying:this.editPromotion.overlying,
                thumbnail:this.editPromotion.thumbnail,
                title:this.editPromotion.title,
                type:this.editPromotion.type
              }
              if (this.editPromotion.type!=0&&this.editPromotion.type!=1&&this.editPromotion.type!=2&&this.editPromotion.type!=5){
                this.giftTypeList=[]
              }else{
                if (this.editPromotion.giftType===0){
                  if (this.$refs.editChild.gifts.length&&this.$refs.editChild.gifts.length>0){
                    this.giftTypeList=this.$refs.editChild.gifts
                  }else{
                    this.giftTypeList=[]
                  }
                }else if (this.editPromotion.giftType===1){
                  if (this.$refs.editCouponAdd.coupons&&this.$refs.editCouponAdd.coupons.length>0){
                    this.giftTypeList=this.$refs.editCouponAdd.coupons
                  }else{
                    this.giftTypeList=[]
                  }
                }
              }
              editPromotion.gifts=this.giftTypeList
              if(this.dateTime.length >0){
                editPromotion.beginDate = this.dateTime[0];
                editPromotion.endDate = this.dateTime[1]
              }
              if (this.editPromotion.type===6){
                editPromotion.title='满'+editPromotion.minimumPrice+'包邮'
              }else if (this.editPromotion.type===3){
                editPromotion.title='满'+editPromotion.minimumPrice+'减'+editPromotion.amount
              }else if (this.editPromotion.type===4){
                editPromotion.title='满'+editPromotion.minimumPrice+'打'+editPromotion.agioRate+'折'
              }
              this.editVisible = false;

              updatePromotion(editPromotion).then(response =>{
                EventBus.$emit('promotion-edit', response.data)
                EventBus.$emit('productPromotion-edit', response.data)
              }).catch(error =>{
                this.editVisible = false;
                console.log('error')
              })
              this.$emit('loadPromotion')
            })
          }else{
            this.editVisible = false;
          }
        })
      },
      findPromotion(id){
        find(id).then(response =>{
          this.editPromotion=response.data.promotion;
          this.dateTime.push(this.editPromotion.beginDate);
          this.dateTime.push(this.editPromotion.endDate);
        })
        this.isStatus=true
      },

    },
    filters:{
      watchTrue(val){
        if (val==true){
          return '是'
        }else if (val==false){
          return '否'
        }
      },
      watchGiftType(val){
        if (val==0){
          return '礼品'
        }else if(val==1){
          return '优惠券'
        }
      },
      watchType(val){
        const  typeName=['新人礼','消费送','扫码送','满减','满折','满赠','满包邮','买送','秒杀','第二件半价','新人价'];
        return typeName[val];
      },
      watchAmountType(val){
        if (val==3){
          return '满减金额：'
        }else if (val==7){
          return '赠送数量：'
        }else if (val===8){
          return '秒杀价格：'
        }else if (val===10){
          return '新人价：'
        }
      }
    },
    components:{SingleUpload,giftList,coupon,editGift,editCoupon},
  }
</script>

<style scoped>
  .popup_content {
    padding-bottom: 0px !important;
  }
  .el-footerBox {
    height: 50px !important;
    line-height: 50px !important;
    border-top: 1px solid #eeeeee;
  }

  /deep/ .el-dialog__header {
    border: 1px solid #eeeeee !important;
    padding: 15px !important;
  }

  /deep/ .el-dialog__body {
    padding: 0px 20px !important;
  }
</style>
